#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
    umask(0);
    int wfd = open("./hello.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
    if (wfd == -1)
    {
        perror("open false1");
        close(wfd);
        return 1;
    }
    char arr[] = "i like linux";
    int count = sizeof(arr) / sizeof(arr[0]);
    ssize_t wsz = write(wfd, arr, count);
    if (wsz == -1)
    {
        perror("Error writing to file");
        close(wfd);
        return 1;
    }
    close(wfd);
    printf("Wrote %zd bytes to the file.\n", wsz);

    // read
    int rfd = open("./bite.txt", O_RDONLY, 0666);
    if (rfd == -1)
    {
        perror("open false2");
        close(rfd);
        return 1;
    }
    char buf[64];
    ssize_t rsz = read(rfd, buf, sizeof(buf) - 1);
    if (rsz == -1)
    {
        perror("read false");
        close(rfd);
        return 1;
    }
    buf[rsz] = '\0';
    printf("rsz : %zd ,buf : %s\n", rsz, buf);

    // lseek
    //从第一个的字符开始，不是第0个
    off_t new_pos = lseek(rfd, 1, SEEK_SET);
    if (new_pos == -1)
    {
        perror("lseek false");
        close(rfd);
        return 1;
    }
    char buf2[30];
    rsz = read(rfd, buf2, sizeof(buf2) - 1);
    if (rsz == -1)
    {
        perror("read false");
        close(rfd);
        return 1;
    }
    buf2[rsz] = 0;
    printf("lseek :%s\n", buf2);

    close(rfd);
    return 0;
}